<?php

namespace app\level;

use app\payment\PayFactory;
use app\services\ConfigService;
use app\services\PropertyService;
use plugin\admin\app\model\User;
use plugin\admin\app\model\UserPropertyLog;
use plugin\admin\app\model\UserTeam;

class Level
{
    protected $name = '';

    protected $levelInfo = [];

    protected $userId;

    public function __construct($userId, $levelInfo)
    {
        $this->userId = $userId;
        $this->levelInfo = $levelInfo;
    }

    // 注册返利
    public function registerReward()
    {
        PropertyService::add($this->userId, PayFactory::LOVE, $this->levelInfo['recommend_love'], UserPropertyLog::RECOMMEND_REGISTER);
    }

    // 满足推荐人数给功德章
    public function registerRewardGD()
    {
        $config = ConfigService::get('site');

        $userTeam = UserTeam::find($this->userId);

        if (($userTeam['recommend_count'] + 1)%$config['gd_recommend_count'] == 0) {
            PropertyService::add($this->userId, PayFactory::GD, $config['gd_count'], UserPropertyLog::INVITE_GD);
        }
    }

    // 直推消费奖励
    public function recommendRebate($money, $orderNo = '')
    {
        if ($money > 0) {
            $recommendRebate = bcmul($money, ($this->levelInfo['recommend_rate'] / 100), 2);

            if ($recommendRebate > 0) {
                PropertyService::add($this->userId, PayFactory::BALANCE, $recommendRebate, UserPropertyLog::RECOMMEND_REBATE, '', $orderNo, 1);
            }

        }
    }

    // 间推消费奖励
    public function indirectRecommendRebate($money, $orderNo = '')
    {
        if ($money > 0) {
            $indirectRecommendRebate = bcmul($money, ($this->levelInfo['indirect_recommend_rate'] / 100), 2);

            if ($indirectRecommendRebate > 0) {
                PropertyService::add($this->userId, PayFactory::BALANCE, $indirectRecommendRebate, UserPropertyLog::INDIRECT_RECOMMEND_REBATE, '', $orderNo, 2);
            }

        }
    }

    // 直推购买礼包奖励
    public function recommendGiftRebate($money, $orderNo = '', $levelInfo = [])
    {
        if ($money > 0) {
            $recommendRebate = $levelInfo['recommend_gift_rebate'];

            PropertyService::add($this->userId, PayFactory::BALANCE, $recommendRebate, UserPropertyLog::RECOMMEND_GIFT_REBATE, '', $orderNo, 1);
        }
    }

    // 间推购买礼包奖励
    public function indirectRecommendGiftRebate($money, $orderNo = '', $levelInfo = [])
    {
        if ($money > 0) {
            $indirectRecommendRebate = $levelInfo['indirect_recommend_gift_rebate'];

            if ($indirectRecommendRebate > 0) {
                PropertyService::add($this->userId, PayFactory::BALANCE, $indirectRecommendRebate, UserPropertyLog::INDIRECT_RECOMMEND_GIFT_REBATE, '', $orderNo, 2);
            }

        }
    }

    // 直推申请名片奖励
    public function recommendOpenNameCardRebate($money)
    {
        if ($money > 0) {
            if ($this->levelInfo['invite_recommend_amount'] > 0) {
                PropertyService::add($this->userId, PayFactory::BALANCE, $this->levelInfo['invite_recommend_amount'], UserPropertyLog::RECOMMEND_OPEN_NAME_CARD_REBATE);
            }
        }
    }

    // 间推申请名片奖励
    public function indirectRecommendOpenNameCardRebate($money)
    {
        if ($money > 0) {
            if ($this->levelInfo['invite_indirect_recommend_amount'] > 0) {
                PropertyService::add($this->userId, PayFactory::BALANCE, $this->levelInfo['invite_indirect_recommend_amount'], UserPropertyLog::INDIRECT_RECOMMEND_OPEN_NAME_CARD_REBATE);
            }
        }
    }
}